﻿using System;
using System.Web;
using System.Web.Mvc;

namespace SlamCms.Common
{
	public static class StringExtensions
	{
		public static bool IsNullOrWhiteSpace(this string inputString)
		{
			return string.IsNullOrWhiteSpace(inputString);
		}

		public static IHtmlString ToHtml(this string inputString)
		{
			return MvcHtmlString.Create(inputString);
		}

		public static IHtmlString ToUrl(this string input, IHtmlString baseUrl)
		{
			return input.ToUrl(baseUrl.ToHtmlString());
		}

		public static IHtmlString ToUrl(this string input, string baseUrl = null)
		{
			Uri uri;
			if (Uri.TryCreate(input, UriKind.Absolute, out uri))
				return uri.ToString().ToHtml();
			else if (baseUrl == null && Uri.TryCreate(input, UriKind.Relative, out uri))
				return uri.ToString().ToHtml();
			else if (baseUrl == null)
				return input.ToHtml();
			else if (Uri.TryCreate(new Uri(baseUrl, UriKind.Absolute), input, out uri))
				return uri.ToString().ToHtml();
			else
				return (baseUrl + input).ToHtml();
		}
	}
}